<?php

require_once PATH_CLASS.'theme.class.php';

abstract class sitePage{
  const C_LEFT              = 'left';
  const C_MIDDLE            = 'middle';
  const C_RIGHT             = 'right';
  protected $_page          = 'accueil';
  protected $_smarty        = null;
  protected $_renderDefault = true;

  public function __construct(Smarty &$smarty) {
    $this->_smarty = $smarty;
    $this->init();
    $smarty->assignByRef('renderPage', $this);
    $this->setPage(get_class($this));
    $this->playTheme();
 }
 
  public function init(){
    
  }
  
  public function playTheme(){
    theme::playRandomTheme();
  }
  
  public function getSmarty(){
    return $this->_smarty;
  }
  
  public function setRenderDefault($st){
    $this->_renderDefault = $st;
    return $this;
  }

  public function getRenderDefault(){
    return $this->_renderDefault;
  }

  public function setPage($page){
    $this->_page = $page;
    return $this;
  }
  
  public function getPage(){
    return $this->_page;
  }
  
  public function getLayout(){
    return 'site_layout.'.TPL_EXT;
  }
  
  protected function _fetch($mode){
    $page = $this->getPage();
    $classPath=PATH_PAGE.$page."/{$mode}.content.php";
    $className="{$page}_{$mode}";
    $ret      ='';
    try{
      if(file_exists($classPath)){
        require_once $classPath;
        $content = new $className($this);
        $ret = $content->render();
      } else {
        $classPath = PATH_PAGE.'defaultPage.page.php';
        if(file_exists($classPath)){
          require_once $classPath;
          $content = new defaultPage($this->_smarty);
          $ret = $content->fetch($mode);
          $this->_smarty->assign_by_ref('renderPage', $this);
          $this->playTheme();
        }
      }
    } catch(Exception $e){
    }
    
    return $ret;
  }

  public function fetchLeft(){
    if($this->hasLeft()){
      return $this->_fetch(self::C_LEFT);
    }
    return '';
  }

  public function fetchMiddle(){
    if($this->hasMiddle()){
      return $this->_fetch(self::C_MIDDLE);
    }
    return '';
  }
  
  public function fetchRight(){
    if($this->hasRight()){
      return $this->_fetch(self::C_RIGHT);
    }
    return '';
  }
  
  public function fetch($mode){
    
    switch($mode){
      case self::C_LEFT:
        $ret = $this->fetchLeft();
        break;
      case self::C_MIDDLE:
        $ret = $this->fetchMiddle();
        break;
      case self::C_RIGHT:
        $ret = $this->fetchRight();
        break;
    }
    
    
    return $ret;
  }
  
  public function render($mode){
    echo $this->fetch($mode);
  }
  
  public function toString(){
    $ret =get_class($this);
    $ret = join(',', get_class_methods($this));
    return $ret;
  }
  
  abstract function hasLeft();
  abstract function hasMiddle();
  abstract function hasRight();
  
}

